package com.ruoyi.web.controller.message;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.message.domain.LeaveMessage;
import com.ruoyi.message.service.ILeaveMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 留言管理Controller
 * 
 * @author chen
 * @date 2023-12-13
 */
@RestController
@RequestMapping("/message/message")
public class LeaveMessageController extends BaseController
{
    @Autowired
    private ILeaveMessageService leaveMessageService;

    /**
     * 查询留言管理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveMessage leaveMessage)
    {
        startPage();
        List<LeaveMessage> list = leaveMessageService.selectLeaveMessageList(leaveMessage);
        return getDataTable(list);
    }

    /**
     * 导出留言管理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:export')")
    @Log(title = "留言管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveMessage leaveMessage)
    {
        List<LeaveMessage> list = leaveMessageService.selectLeaveMessageList(leaveMessage);
        ExcelUtil<LeaveMessage> util = new ExcelUtil<LeaveMessage>(LeaveMessage.class);
        util.exportExcel(response, list, "留言管理数据");
    }

    /**
     * 获取留言管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveMessageService.selectLeaveMessageById(id));
    }

    /**
     * 新增留言管理
     */
    @PreAuthorize("@ss.hasPermi('message:message:add')")
    @Log(title = "留言管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveMessage leaveMessage)
    {
        leaveMessage.setStatus(0);
        return toAjax(leaveMessageService.insertLeaveMessage(leaveMessage));
    }

    /**
     * 修改留言管理
     */
    @PreAuthorize("@ss.hasPermi('message:message:edit')")
    @Log(title = "留言管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveMessage leaveMessage)
    {
        return toAjax(leaveMessageService.updateLeaveMessage(leaveMessage));
    }

    /**
     * 删除留言管理
     */
    @PreAuthorize("@ss.hasPermi('message:message:remove')")
    @Log(title = "留言管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(leaveMessageService.deleteLeaveMessageByIds(ids));
    }


    /**
     * 审核留言
     */
    @PreAuthorize("@ss.hasPermi('message:message:examine')")
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody LeaveMessage leaveMessage)
    {
        return toAjax(leaveMessageService.examineLeaveMessage(leaveMessage));
    }

    /**
     * 批量审核审核通过
     */
    @PreAuthorize("@ss.hasPermi('message:message:examine')")
    @PostMapping("/batchExaminePass/{ids}")
    public AjaxResult batchExaminePass(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            LeaveMessage leaveMessage = leaveMessageService.selectLeaveMessageById(id);
            leaveMessage.setStatus(1);
            leaveMessageService.examineLeaveMessage(leaveMessage);
        }
        return toAjax(1);
    }

    /**
     * 批量审核审核失败
     */
    @PreAuthorize("@ss.hasPermi('message:message:examine')")
    @PostMapping("/batchExamineFail/{ids}")
    public AjaxResult batchExamineFail(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            LeaveMessage leaveMessage = leaveMessageService.selectLeaveMessageById(id);
            leaveMessage.setStatus(2);
            leaveMessageService.examineLeaveMessage(leaveMessage);
        }
        return toAjax(1);
    }
}
